package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.QueryPageBeanT;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("queryItem")
    @ResponseBody
    public Result queryItem(){
        List<CheckItem> checkItems = checkItemService.queryItemlist();
        return new Result(true,"",checkItems);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(@RequestBody CheckGroup checkGroup, Integer [] checkitemIds){


        try {
            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return  new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("queryPage")
    public PageResult query(@RequestBody QueryPageBeanT queryPageBean){
        PageResult pageResult = checkGroupService.queryPage(queryPageBean);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("findone")
    public Result findone(Integer id){
        CheckGroup checkGroup = checkGroupService.findone(id);

        return new Result(true,"",checkGroup);
    }

    @ResponseBody
    @RequestMapping("queryItemByid")
    public Result queryItemByid(Integer id){
        List<Integer> integers = checkGroupService.queryItemByid(id);
        return new Result(true,"",integers);
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(@RequestBody CheckGroup checkGroup, Integer [] checkitemIds){


        try {
            checkGroupService.update(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return  new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);

    }
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer id){
        Result result = checkGroupService.delete(id);
        return result;
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Result findAll(){
        try{
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }

}
